package com.barunlab.ems.server.main;

import java.io.Serializable;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.barunlab.ems.library.PropertyLoader;

/**
 * 서버 properties 관리 클래스
 * 
 * @author 배수현
 * @version R1.0
 * @since JDK6.0
 * @created 2013
 */
public class ServerProperties implements Serializable {
	private static Logger LOGGER = Logger.getLogger(ServerProperties.class);
	
	private static String[] filePath;
	private static Properties[] props;
	
	public static int 		RMI_PORT;
	public static String 	RMI_SMS_BIND_NAME;
	public static long		CLIENT_HEARTBEAT_DURATION;
	public static int		CLIENT_SEND_FAIL_COUNT;
	
	public static void load(String[] filePath) throws Exception {
		ServerProperties.filePath = filePath;
		
		int filePathNum = filePath.length;
		
		ServerProperties.props = new Properties[filePathNum];
		for ( int i = 0; i < filePathNum; i++ ) {
			ServerProperties.props[i] = PropertyLoader.load(filePath[i]);
		}
		
		RMI_PORT 					= getIntProperty("server.rmi.port");
		RMI_SMS_BIND_NAME 			= getStringProperty("server.rmi.sms.bind_name");
		CLIENT_HEARTBEAT_DURATION	= getLongProperty("server.session.heartbeat");
		CLIENT_SEND_FAIL_COUNT		= getIntProperty("server.session.sendfail");
	}
	
	/**
	 * key에 해당하는 integer형 property를 반환한다.
	 * 
	 * @param key
	 * @return property 값
	 * @throws Exception
	 */
	public static int getIntProperty(String key) throws Exception {
		int value = 0;
		
		String valueStr = (String)getProperty(key);
		try {
			value = Integer.parseInt(valueStr);
		}
		catch ( NumberFormatException nfe ) {
			String msg = String.format("%s(%s) is not valid.", key, valueStr);
			throw new Exception(msg);
		}
		
		LOGGER.info(String.format("%s=%s", key, value));
		
		return value;
	}
	
	/**
	 * key에 해당하는 long형 property를 반환한다.
	 * 
	 * @param key
	 * @return property 값
	 * @throws Exception
	 */
	public static long getLongProperty(String key) throws Exception {
		long value = 0;
		
		String valueStr = (String)getProperty(key);
		try {
			value = Long.parseLong(valueStr);
		}
		catch ( NumberFormatException nfe ) {
			String msg = String.format("%s(%s) is not valid.", key, valueStr);
			throw new Exception(msg);
		}
		
		LOGGER.info(String.format("%s=%s", key, value));
		
		return value;
	}
	
	/**
	 * key에 해당하는 string형 property를 반환한다.
	 * 
	 * @param key
	 * @return property 값
	 * @throws Exception
	 */
	public static String getStringProperty(String key) throws Exception {
		String value = (String)getProperty(key);
		
		LOGGER.info(String.format("%s=%s", key, value));
		
		return value;
	}
	
	/**
	 * key에 해당하는 boolean형 property를 반환한다.
	 * 
	 * @param key
	 * @return property 값
	 * @throws Exception
	 */
	public static boolean getBooleanProperty(String key) throws Exception {
		String valueStr = (String)getProperty(key);
		
		LOGGER.info(String.format("%s=%s", key, valueStr));
		
		return "true".equalsIgnoreCase(valueStr) ? true : false;
	}
	
	/**
	 * key에 해당하는 Object형 property를 반환한다.
	 * 
	 * @param key
	 * @return property 값
	 * @throws Exception
	 */
	public static Object getProperty(Object key) throws Exception {
		int filePathNum = filePath.length;
		
		StringBuffer pathBuffer = new StringBuffer();
		
		Object value = null;
		for ( int i = 0; i < filePathNum; i++ ) {
			if ( i > 0 ) {
				pathBuffer.append(",");
			}
			pathBuffer.append(filePath[i]);
			
			Object tempValue = props[i].get(key);
			if ( value == null && tempValue != null ) {
				value = tempValue;
			}
		}
		
		if ( value == null ) {
			String msg = String.format("\"%s\" does not exists in config(%s).", key, pathBuffer);
			throw new Exception(msg);
		}
		
		return value;
	}
	
	
}
